<?php

namespace App\Jobs\Mp;

use App\Lib\WeApp\WeApp;
use App\Models\Mp\WxMpFreePublish;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class PublishGetStatusJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $publish_id = '';
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($publish_id)
    {
        $this->publish_id = $publish_id;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        if(_empty_($this->publish_id)){
            return;
        }
        try {
            $weapp = new WeApp('mp');
            $mp = $weapp->getMpServicer();
            $res = $mp->getPublishStatus($this->publish_id);
            if($res['code'] == 200){
                $updates = [
                    'publish_status' => $res['publish_status'],
                    'article_id' => $res['article_id'] ?? '',
                    'article_url' => $res['article_url'] ?? ''
                ];
                if(!_empty_($updates['article_id'])){
                    $updates['media_id'] = null;
                }
                WxMpFreePublish::where('publish_id', $this->publish_id)->update($updates);
            }else{
                _logger_(__file__, __line__, $res['msg']);
            }
        }catch (\Exception $e){
            _logger_(__file__, __line__, $e->getMessage());
            _logger_(__file__, __line__, '请尝试重启进程[队列]');
        }

    }
}
